package string;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**并发安全的集合：
 * synchronizedList、synchronizedSet、synchronizedMap转换
 * List(ArrayList、LinkedList)，Set(HashSet、linkedHashSet)，Map(HashMap、linkedHashMap)
 * */
public class SyncApiDemo2 {
    public static void main(String[] args) {
        List<Integer> l=new ArrayList<>();
       // List<Integer> l=Collections.synchronizedList(new ArrayList<>());
        Thread t1=new Thread(){
            public void run(){
                for (int i=0;i<1000;i++){
                    l.add(i);
                }
            }
        };
        Thread t2=new Thread(){
            public void run(){
                for (int i=0;i<1000;i++){
                    l.add(i);
                }
            }
        };
        t1.start();
        t2.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(l.size());
    }
}
